<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button
          type="primary"
          @click="handleForm({})"
          v-auth="'mesVisual:visual:visualKkMonthData:edit'"
        >
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
        <a-button
          type="success"
          @click="handleFb({})"
          v-auth="'mesVisual:visual:visualKkMonthData:edit'"
        >
          <Icon icon="ant-design:check-circle-outlined" /> {{ t('发布') }}
        </a-button>
        <a-popconfirm
          title="是否确认取消发布？"
          ok-text="是"
          cancel-text="否"
          placement="bottom"
          @confirm="handleCancelFb()"
        >
          <a-button type="danger" v-auth="'mesVisual:visual:visualKkMonthData:edit'">
            <Icon icon="ant-design:close-circle-filled" /> {{ t('取消发布') }}
          </a-button>
        </a-popconfirm>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id, workShop: record.workShop, title: record.title })">
          {{ record.title }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsmesVisualVisualVisualKkMonthDataList">
  import { unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    visualKkMonthDataDelete,
    visualKkMonthDataListData,
    visualKkMonthDataPublish,
    visualKkMonthDataUnPublish,
  } from '/@/api/mesvisual/visual/visualKkMonthData';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';

  const { t } = useI18n('mesVisual.visual.visualKkMonthData');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('车间看板数据展示管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('标题'),
        field: 'title',
        component: 'Input',
      },
      {
        label: t('上传日期'),
        field: 'uploadDate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'msg_inner_msg_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('创建人名称'),
        field: 'createByName',
        component: 'Input',
      },
      {
        label: t('备注'),
        field: 'remarks',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('标题'),
      dataIndex: 'title',
      key: 'a.title',
      sorter: true,
      width: 230,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('车间'),
      dataIndex: 'workShop',
      key: 'a.work_shop',
      sorter: true,
      width: 130,
      align: 'left',
      dictType: 'srm_work_shop',
    },
    {
      title: t('上传日期'),
      dataIndex: 'uploadDate',
      key: 'a.upload_date',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'msg_inner_msg_status',
    },
    {
      title: t('创建人'),
      dataIndex: 'createByName',
      key: 'a.create_by_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('备注'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 230,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑车间看板数据展示'),
        onClick: handleForm.bind(this, {
          id: record.id,
          workShop: record.workShop,
          title: record.title,
        }),
        auth: 'mesVisual:visual:visualKkMonthData:edit',
        ifShow: () => record.status === '9',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除车间看板数据展示'),
        popConfirm: {
          title: t('是否确认删除车间看板数据展示'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'mesVisual:visual:visualKkMonthData:edit',
        ifShow: () => record.status === '9',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getSelectRowKeys }] = useTable({
    api: visualKkMonthDataListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const params = { id: record.id, workShop: record.workShop, title: record.title };
    const res = await visualKkMonthDataDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  // 发布
  async function handleFb() {
    if (getSelectRowKeys().length) {
      const res = await visualKkMonthDataPublish({ selIds: getSelectRowKeys().join(',') });
      showMessage(res.message);
      handleSuccess();
    } else {
      showMessage('请先选择一条记录！');
    }
  }

  // 取消发布
  async function handleCancelFb() {
    if (getSelectRowKeys().length) {
      const res = await visualKkMonthDataUnPublish({ selIds: getSelectRowKeys().join(',') });
      showMessage(res.message);
      handleSuccess();
    } else {
      showMessage('请先选择一条记录！');
    }
  }

  function handleSuccess() {
    reload();
  }
</script>
